import { defineConfig, loadEnv, type ConfigEnv } from "vite";
import { resolve } from "path";

import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";

// Vant
// import Components from "unplugin-vue-components/vite";
// import { VantResolver } from "unplugin-vue-components/resolvers";

// NutUI
import { createStyleImportPlugin } from "vite-plugin-style-import";

// https://vitejs.dev/config/
export default defineConfig((mode: ConfigEnv) => {
  const env = loadEnv(mode.mode, process.cwd());
  return {
    plugins: [
      vue(),
      // Vant按需引入插件配置
      // Components({
      //   resolvers: [VantResolver()],
      // }),
      // NutUI按需引入插件配置
      createStyleImportPlugin({
        resolves: [
          {
            libraryName: "@nutui/nutui",
            libraryNameChangeCase: "pascalCase",
            resolveStyle: (name) => {
              return `@nutui/nutui/dist/packages/${name.toLowerCase()}/index.scss`;
            },
          },
        ],
      }),
      vueJsx(),
    ],
    resolve: {
      alias: {
        "@": resolve(__dirname, "src"),
      },
      extensions: [".ts", ".vue", ".js", ".jsx", ".tsx"], // 导入时想要省略的扩展名列表。
    },
    css: {
      preprocessorOptions: {
        scss: {
          // 引入 var.scss 这样就可以在全局中使用 var.scss中预定义的变量了

          // 给导入的路径最后加上 ;
          additionalData: `@import "@nutui/nutui/dist/styles/variables.scss";`,

        },
      },
      postcss: {
        plugins: [
          {
            postcssPlugin: "internal:charset-removal",
            AtRule: {
              charset: (atRule) => {
                if (atRule.name === "charset") {
                  atRule.remove();
                }
              },
            },
          },
        ],
      },
    },
    server: {
      proxy: {
        // 选项写法
        "/app-dev": {
          target: "http://sph-h5-api.atguigu.cn", // 代理的目标路径
          changeOrigin: true, // 允许跨域
          rewrite: (path) => path.replace(/^\/app-dev/, ""), // 路径重写
        },
        "/wy": {
          target: "https://m.you.163.com", // 代理的目标路径
          changeOrigin: true, // 允许跨域
          rewrite: (path) => path.replace(/^\/wy/, ""), // 路径重写
        },
      },
     
    }
  };
});
